package com.fastloan.mgt.model.service.arithmetic.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fastloan.mgt.application.base.model.Application;
import com.fastloan.mgt.application.contract.model.ApplicationContract;
import com.fastloan.mgt.customer.model.ZyCustomer;
import com.fastloan.mgt.model.annotation.ModelClass;
import com.fastloan.mgt.model.model.ApplicationModel;
import com.fastloan.mgt.model.service.GeneratorModelStrategy;
import com.fastloan.mgt.model.uitl.ModelUtil;
import com.fastloan.mgt.product.basics.model.Product;
import com.fastloan.mgt.product.basics.service.ProductService;
import com.fastloan.mgt.verify.enums.ApiStore;
import com.fastloan.mgt.verify.model.ApiResult;
import com.fastloan.mgt.verify.util.ApiUtil;
import com.fastloan.mgt.verify.util.GetParam;
import com.fastloan.utils.PropertiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Random;
import java.util.function.Consumer;

import static com.fastloan.mgt.model.service.impl.ModelCalculateServiceImpl.NO_PASS;
import static com.fastloan.mgt.model.service.impl.ModelCalculateServiceImpl.PASS;

/**
 * @author yanwe
 * createTime 2017-12-2017/12/20 18:00
 */
@Component
@ModelClass(modelName = "消费模型",modelId = 3)
public class ConsumerFinanceModel extends GeneratorModelStrategy {

    @Autowired
    private ProductService productService;

    @Override
    public void calculate(String applicationId, Consumer<ApplicationModel> applicationModelConsumer) {
        ApplicationModel applicationModel = createApplicationModel(applicationId);
        Application applicationInfo = getApplicationInfo(applicationId);
        ZyCustomer customerInfo = getCustomerInfo(applicationInfo.getCustomerId());
        ApiResult apiResult = ApiUtil.sendGetSync(GetParam.newBuilder()
                        .add("personalId", customerInfo.getCardNum())
                        .add("name", customerInfo.getCname())
                        .add("api_key", PropertiesUtil.getProperty("verify.apiKey"))
                        .add("mobileNum", customerInfo.getPhoneNum())
                , ApiStore.BAIDU_SCORE);
        JSONObject jsonObject = JSON.parseObject(apiResult.getData());
        JSONObject result = jsonObject.getJSONObject("result");
        Integer creditScore = result.getInteger("creditScore");
        BigDecimal modelResult = BigDecimal.ZERO;
        if (creditScore == -1 ||creditScore < 540){
            modelResult = BigDecimal.ZERO;
        } else if (creditScore >= 743){
            modelResult = BigDecimal.valueOf(50000);
        } else if(creditScore >= 540){
            modelResult = BigDecimal.valueOf(Math.pow((creditScore-540),2)).divide(BigDecimal.valueOf(41209),2)
                    .multiply(BigDecimal.valueOf(50000));
        }
        //取得产品下限
        Product product = productService.selectByPrimaryKey(applicationInfo.getProductId());
        BigDecimal spendingFloor = product.getSpendingFloor();
        //取整授信额度
        BigDecimal creditLine = ModelUtil.disposeResult(modelResult, 1, BigDecimal.ROUND_DOWN);
        //模型结果比产品下限,小则模型未通过
        if (modelResult.compareTo(spendingFloor) < 0){
            applicationModel.setPass(NO_PASS);
            applicationModel.setModelResult(creditLine);
        } else {
            applicationModel.setPass(PASS);
            //否则模型通过并对比申请额度取最小值
            applicationModel.setModelResult(creditLine.min(applicationInfo.getApplyAmount()));
        }
        applicationModelConsumer.accept(applicationModel);
    }

    @Override
    public void sendBack(ApplicationContract applicationContract, boolean isPass) {

    }

}
